package com.ww.springboot.boot.thread.study.executor;

import lombok.SneakyThrows;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.*;

/**
 * 只有核心线程已满 队列已满 才会创建【不超过最大线程数的】额外的线程 除非调整默认拒绝策略 否则使用意义不大
 * <p>
 * LinkedBlockingQueue 比 ArrayBlockingQueue更常用
 *
 * @author wanwei
 * @since 2021-04-20 11:03
 */
public class ExecutorDemo3 {


    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = new ThreadPoolExecutor(8, 16,
                1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(84), new CustomizableThreadFactory("price-thread-pool"));
        for (int i = 0; i < 100; i++) {
            executorService.execute(new ThreadDemo());
        }
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            TimeUnit.SECONDS.sleep(1);
        }
    }

    static class ThreadDemo implements Runnable {

        @SneakyThrows
        @Override
        public void run() {
            System.out.println(Thread.currentThread());
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
